package com.wzq.app.utils;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;


import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

public class FilePathUtils {
    public static String APP_PATH;

    public static void initFilePath(Context context) {
        APP_PATH = Environment.getExternalStorageDirectory().getPath()
                + File.separator + "fuhuang" + File.separator
                + context.getPackageName();
        if (!FileUtils.fileIsExist(APP_PATH)) {
            new File(APP_PATH).mkdirs();
        }
    }
    public static String getQrcodeFolder(){
        String path = APP_PATH + File.separator + "qrcode";
        if(!FileUtils.fileIsExist(path)){
            new File(path).mkdirs();
        }
        return path;
    }

    public static String getImageFolder(){
        String path = APP_PATH + File.separator + "image";
        if(!FileUtils.fileIsExist(path)){
            new File(path).mkdirs();
        }
        return path;
    }

    public static String getCropImagePath() {
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = getImageFolder()+ File.separator+timeStamp+"CROP_JPEG.jpg";
        return imageFileName;
    }

    public static String getCachePath() {
        String path = APP_PATH + File.separator + "cache";
        if (!FileUtils.fileIsExist(path)) {
            new File(path).mkdirs();
        }
        return path;
    }

    public static String getCrashPath() {
        String path = APP_PATH + File.separator + "crash";
        if (!FileUtils.fileIsExist(path)) {
            new File(path).mkdirs();
        }
        return path;
    }

    public static Uri FilePathToUri(Context context, String path) {

        if (path != null) {
            path = Uri.decode(path);
            ContentResolver cr = context.getContentResolver();
            StringBuffer buff = new StringBuffer();
            buff.append("(")
                    .append(MediaStore.Images.ImageColumns.DATA)
                    .append("=")
                    .append("'" + path + "'")
                    .append(")");
            Cursor cur = cr.query(
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                    new String[]{MediaStore.Images.ImageColumns._ID},
                    buff.toString(), null, null);
            int index = 0;
            for (cur.moveToFirst(); !cur.isAfterLast(); cur
                    .moveToNext()) {
                index = cur.getColumnIndex(MediaStore.Images.ImageColumns._ID);
                // set _id value
                index = cur.getInt(index);
            }
            if (index == 0) {
                //do nothing
            } else {
                Uri uri_temp = Uri
                        .parse("content://media/external/images/media/"
                                + index);
                if (uri_temp != null) {
                    return uri_temp;
                }
            }

        }
        return null;
    }
}
